/**
 * 
 */
package action.admin;

import java.util.List;
import java.util.Map;

import model.CoursetypeDAO;
import model.DepartmentDAO;
import model.TeacherDAO;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionContext;

import model.*;
/** 
 * @作者 Zouzr 
 * @创建日期 Dec 18, 2008 
 * @版本 V 1.0 
 */

public class LoginAction extends ActionSupport 
{
	private String username;
	
	private String userpassword;
	
	private String checkcode;
	
	private String message="";
	
	/**
	 * 修改密码的属性
	 */
	private String userpasswordold;//旧密码
	
	private String userpasswordnew;//新密码
	
	private String userpasswordchecknew;//确认新密码
	
	private ServletActionContext context;
	
/**
 * 验证登陆
 * @return
 */
	public String adminLogin()
	{
		if(username.equals("")||username==null)
		{
			message=message+"用户名未填写<br>";
			return SUCCESS;
		}
		if(userpassword.equals("")||userpassword==null)
		{
			message=message+"密码未填写<br>";
			return SUCCESS;
		}
		if(checkcode.equals("")||checkcode==null)
		{
			message=message+"验证码未填写<br>";
			return SUCCESS;
		}
		
		ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        AdminDAO admindao = (AdminDAO)context.getBean("AdminDAOProxy");
        String result=admindao.loginValidate(username, userpassword);
        if(result.equals("UserNotExist"))
        {
        	message=message+"用户不存在<br>";
			return SUCCESS;
        }
        if(result.equals("PasswordError"))
        {
        	message=message+"密码错误<br>";
			return SUCCESS;
        }
        Map map = ActionContext.getContext().getSession();
        String temp_checkcode = (String) map.get("randCode");
        if(!checkcode.equals(temp_checkcode))
		{
			message=message+"验证码错误<br>";
			return SUCCESS;
		}
        System.out.println("result---------"+result);
        this.context.getRequest().getSession().setAttribute("adminId", result);
        this.context.getRequest().getSession().setAttribute("username", username);
        System.out.println("----------"+ this.context.getRequest().getSession().getAttribute("username"));
        return SUCCESS;
	
	}
	
	/**
	 * 退出
	 * @return
	 */
	public String adminLogout()
	{
		this.context.getRequest().getSession().setAttribute("adminId", "");
		this.context.getRequest().getSession().setAttribute("username", "");
        System.out.println("退出后----------"+ this.context.getRequest().getSession().getAttribute("adminId"));
        System.out.println("退出后----------"+ this.context.getRequest().getSession().getAttribute("username"));
		return SUCCESS;
	}
	/**
	 * 修改密码
	 */
	public String adminPasswordEdit()
	{
		if(userpasswordold.equals("")||userpasswordold==null)
		{
			message=message+"旧密码未填写<br>";
			return SUCCESS;
		}
		if(userpasswordnew.equals("")||userpasswordnew==null)
		{
			message=message+"新密码未填写<br>";
			return SUCCESS;
		}
		if(userpasswordchecknew.equals("")||userpasswordchecknew==null)
		{
			message=message+"确认新密码未填写<br>";
			return SUCCESS;
		}
		if(checkcode.equals("")||checkcode==null)
		{
			message=message+"验证码未填写<br>";
			return SUCCESS;
		}
		ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        AdminDAO admindao = (AdminDAO)context.getBean("AdminDAOProxy");
        String result=admindao.loginValidate((String)this.context.getRequest().getSession().getAttribute("username"), userpasswordold);
        if(result.equals("PasswordError"))
        {
        	message=message+"密码错误<br>";
			return SUCCESS;
        }
        if(!userpasswordnew.equals(userpasswordchecknew))
        {
        	message=message+"新密码和确认密码必须一致<br>";
			return SUCCESS;
        }
        Map map = ActionContext.getContext().getSession();
        String temp_checkcode = (String) map.get("randCode");
        if(!checkcode.equals(temp_checkcode))
		{
			message=message+"验证码错误<br>";
			return SUCCESS;
		}
        admindao.editPassword((String)this.context.getRequest().getSession().getAttribute("username"), userpasswordnew);
		return SUCCESS;
	}

	
	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @param username the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @return the userpassword
	 */
	public String getUserpassword() {
		return userpassword;
	}

	/**
	 * @param userpassword the userpassword to set
	 */
	public void setUserpassword(String userpassword) {
		this.userpassword = userpassword;
	}

	/**
	 * @return the checkcode
	 */
	public String getCheckcode() {
		return checkcode;
	}

	/**
	 * @param checkcode the checkcode to set
	 */
	public void setCheckcode(String checkcode) {
		this.checkcode = checkcode;
	}

	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * @return the userpasswordold
	 */
	public String getUserpasswordold() {
		return userpasswordold;
	}

	/**
	 * @param userpasswordold the userpasswordold to set
	 */
	public void setUserpasswordold(String userpasswordold) {
		this.userpasswordold = userpasswordold;
	}

	/**
	 * @return the userpasswordnew
	 */
	public String getUserpasswordnew() {
		return userpasswordnew;
	}

	/**
	 * @param userpasswordnew the userpasswordnew to set
	 */
	public void setUserpasswordnew(String userpasswordnew) {
		this.userpasswordnew = userpasswordnew;
	}

	/**
	 * @return the userpasswordchecknew
	 */
	public String getUserpasswordchecknew() {
		return userpasswordchecknew;
	}

	/**
	 * @param userpasswordchecknew the userpasswordchecknew to set
	 */
	public void setUserpasswordchecknew(String userpasswordchecknew) {
		this.userpasswordchecknew = userpasswordchecknew;
	}
}
